package com.fivetalks.controller.web;

import java.util.ArrayList;
import java.util.List;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fivetalks.dto.CalDTO;


@Controller
public class MeController {
	// /me
	
	@RequestMapping(value="/me", method = RequestMethod.GET)
	public String me(
		Model model
	){
		model.addAttribute("name","홍길동");
		model.addAttribute("age",50);
		return "me";
	}
	
	@RequestMapping(value="/gugu", method = RequestMethod.GET)
	public String gugudan(
		@RequestParam(value="dan", defaultValue="5", required=false)int dan,
		Model model
	){
		List list=new ArrayList();
		for(int i=1;i<10;i++)
		{
			String tmp=dan+"*"+i+"="+(dan*i);
			list.add(tmp);
		}
		model.addAttribute("list",list);
		return "gugu";
	}
	
	@RequestMapping(value="/calform", method = RequestMethod.GET)
	public String calform(
		Model model
	){
		return "calform";
	}
	
	
	@RequestMapping(value="/cal", method = RequestMethod.GET)
	public String cal(
		@RequestParam(value="num1", defaultValue="0", required=false)int num1,
		@RequestParam(value="op", defaultValue="+", required=false)String op,
		@RequestParam(value="num2", defaultValue="0", required=false)int num2,
		Model model
	){
		String result="";
		
		if("+".equals(op)){
			result=num1 + "+" + num2 + "=" + (num1+num2);
		}
		else if("-".equals(op)){
			result=num1 + "-" + num2 + "=" + (num1-num2);
		}
		else if("*".equals(op)){
			result=num1 + "*" + num2 + "=" + (num1*num2);
		}
		else if("/".equals(op)){
			if(num2==0){
				result="0으로 나눌수 없엉ㅋ";
			}
			else{
				result=num1 + "/" + num2 + "=" + (num1/num2);
			}
		}
		model.addAttribute("result",result);
		return "calform";
	}
	
	@RequestMapping(value="/calform2", method = RequestMethod.GET)
	public String calform2(
		Model model
	){
		return "calform2";
	}
	
	
	@RequestMapping(value="/cal2", method = RequestMethod.GET)
	public String cal2(
		@ModelAttribute CalDTO calDTO,
		Model model
	){
		String result="";
		
		if("+".equals(calDTO.getOp())){
			result=calDTO.getNum1() + "+" + calDTO.getNum2() + "=" + (calDTO.getNum1()+calDTO.getNum2());
		}
		else if("-".equals(calDTO.getOp())){
			result=calDTO.getNum1() + "-" + calDTO.getNum2() + "=" + (calDTO.getNum1()-calDTO.getNum2());
		}
		else if("*".equals(calDTO.getOp())){
			result=calDTO.getNum1() + "*" + calDTO.getNum2() + "=" + (calDTO.getNum1()*calDTO.getNum2());
		}
		else if("/".equals(calDTO.getOp())){
			if(calDTO.getNum2()==0){
				result="0으로 나눌수 없엉ㅋ";
			}
			else{
				result=calDTO.getNum1() + "/" + calDTO.getNum2() + "=" + (calDTO.getNum1()/calDTO.getNum2());
			}
		}
		calDTO.setResult(result);
		return "calform2";
	}
}
